package com.demo.security;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.demo.model.JsonMessage;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class JWTAuthenticationEntryPoint implements AuthenticationEntryPoint {
    @Override
    public void commence(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AuthenticationException e) throws IOException{
        httpServletResponse.setCharacterEncoding("UTF-8");
        httpServletResponse.setContentType("application/json; charset=utf-8");
        httpServletResponse.setStatus(HttpServletResponse.SC_OK);
        JsonMessage jsonMessage=new JsonMessage(e.getMessage(),HttpServletResponse.SC_OK,false);
        httpServletResponse.getWriter().write(new ObjectMapper().writeValueAsString(jsonMessage));

    }
}
